/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.example.ventilation.widget.MPChart.chart;

import android.content.Context;
import android.graphics.Color;

import com.example.ventilation.R;
import com.example.ventilation.code.utils.Utils;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;
/**
 * BarChartUtils 柱状图工具类
 * @author gehj
 * @time 1/28/21 9:00 PM
 * https://javadoc.jitpack.io/com/github/PhilJay/MPAndroidChart/v3.1.0/javadoc/
 */
public class BarChartUtils {
//    private static BarChart mBarChart;
    private static List<String> mxValue;

    public static  void BarChartUtils(BarChart chart) {
        //不显示图标 标题
        chart.getLegend().setEnabled(false);
        //关闭描述
        chart.getDescription().setEnabled(false);
        //设置显示值时，最大的柱数量
        chart.setMaxVisibleValueCount(60);
        chart.setDrawValueAboveBar(true);
        //设置不能同时在x轴和y轴上缩放
        chart.setPinchZoom(false);

        chart.setDrawBarShadow(false);
        //设置不画背景网格
        chart.setDrawGridBackground(false);

        //设置X轴样式
        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        chart.getAxisLeft().setDrawGridLines(false);

        YAxis yAxis = chart.getAxisLeft();
        yAxis.setStartAtZero(true);
//
//        //设置图表 标题 的样式
//        Legend l = chart.getLegend();
//        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
//        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
//        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
//        l.setDrawInside(false);
//        l.setForm(Legend.LegendForm.SQUARE);
//        l.setFormSize(9f);
//        l.setTextSize(11f);
//        l.setXEntrySpace(4f);

        chart = chart;
        chart.setNoDataText("暂无数据");
    }
    /**
     * @param datas
     */
    public static void setBarChartData(BarChart chart,List<BarEntry> datas) {
        BarDataSet barDataSet;
        if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
            //有数据时候更新数据
            barDataSet = (BarDataSet) chart.getData().getDataSetByIndex(0);
            barDataSet.setValues(datas);
            chart.getData().notifyDataChanged();
            chart.notifyDataSetChanged();
        } else {//没数据时候初始化数据
            barDataSet = new BarDataSet(datas, "数据集合");
            barDataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
            barDataSet.setDrawValues(true);
            barDataSet.setValueTextSize(12);
//            List<Integer> colors = new ArrayList<>();
//            for(int i=0;i<Utils.CHART_COLORS.length;i++){
//                colors.add(Utils.CHART_COLORS[i]);
//            }
//            barDataSet.setValueTextColors(colors);
            List<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(barDataSet);

            BarData data = new BarData(dataSets);
            chart.setData(data);
            chart.setFitBars(true);
        }
//        if (barDataSet != null) barDataSet.setColor(Color.RED);
        chart.invalidate();
    }

    /**
     * 设置x轴的lable xValues 要与y轴数值对应；
     */
    public static void setXValue(BarChart chart,List<String> xValues) {
        XAxis xAxis = chart.getXAxis();
        xAxis.setLabelRotationAngle(60);
        xAxis.setValueFormatter(new IndexAxisValueFormatter(xValues));
        mxValue = xValues;
        /*xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return super.getFormattedValue(value, axis);
            }
        });*/
    }

    // 更新x轴显示
    public static void updateXAxis(BarChart chart,String[] xdata){
        chart.getXAxis().setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return xdata[(int) value % xdata.length];
            }
        });
    }
    /**
     * 设置y轴的最大值；
     */
    public static void setYValue(BarChart chart,float yMax) {
        YAxis yAxis_left = chart.getAxisLeft();
        yAxis_left.setAxisMaximum(yMax);
        YAxis yAxis_right = chart.getAxisRight();
        yAxis_right.setAxisMaximum(yMax);
    }
    /**
     * 设置y轴的最大值；
     */
    public static void setY_RightVisible(BarChart chart,boolean showFlag) {
        YAxis yAxis_right = chart.getAxisRight();
        yAxis_right.setEnabled(showFlag);
    }

    /**
     * 设置x轴的Label 倾斜；
     */
    public static void setLabelRotationAngle(BarChart chart,float angle) {
        chart.getXAxis().setLabelRotationAngle(angle);
    }

    /**
     * 点击跳出marker，可以不设置
     *
     * @param context
     */
    //TODO 最好是按照XYView X,Y formartor那样 -- 也可以用Toast代替
    //1 implements OnChartValueSelectedListener
//  2 @Override
//  3  public void onValueSelected(Entry e, Highlight h) {
//  4      XToastUtils.toast("选中了,  x轴值:" + e.getX() + ", y轴值:" + e.getY());
    public static void setMarkerView(BarChart chart,Context context) {
        MyMarkerView mv = new MyMarkerView(context, R.layout.custom_marker_view,
                chart, mxValue);
        mv.setChartView(chart); // For bounds control
        chart.setMarker(mv);
    }


}
